package com.fzd.code.file.storage;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.UUID;

public class DefaultFileNameParser implements IFileNameRuleParser {
    public void parse(StorageConfig storageConfig, Source source) {
        if(StringUtils.isEmpty(storageConfig.getFileNameRule())){
            storageConfig.setFileNameRule(
                    DefaultFileNameRule.UUID.name()
            );
        }

        DefaultFileNameRule r = DefaultFileNameRule.valueOf(storageConfig.getFileNameRule());
        switch (r){
            case UUID:
                String uuid = UUID.randomUUID().toString().replaceAll("-","");
                source.setNewFileName(
                                uuid
                                +"."
                                +FilenameUtils.getExtension(source.getOriginalFileName()));
        }
    }
}
